package com.anxin.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "AI聊天")
@Slf4j
@RestController
@RequestMapping("/api/ai")
@RequiredArgsConstructor
public class DeepSeekChatController extends BaseController {

    private final OpenAiChatModel chatModel;

    // 领域限定提示词（核心功能）
    private static final String SYSTEM_PROMPT = """
        你是一个专业的医院预约挂号导诊助手，只处理与医院挂号、科室选择、医生排班相关的咨询。
        禁止回答与医疗咨询、诊断、治疗方案或药品相关的问题。
        若遇到以下类型问题，请拒绝回答并引导用户咨询专业医生：
        1. 疾病诊断
        2. 用药建议
        3. 检查结果解读
        4. 非挂号流程的医疗咨询
        
        你的能力范围包括：
        - 解释预约挂号流程
        - 推荐就诊科室
        - 查询医生出诊时间
        - 说明挂号所需材料
        - 协助取消/改约挂号
        - 解答医保相关问题
        - 解释挂号流程
        - 查询科室信息
        - 提供医院工作时间
        - 辅助病情诊断预约挂号
        
        当前医院信息：
        - 工作时间：8:00-17:30 (周一至周六)
        - 挂号方式：微信小程序/医院窗口/官网
            {
                    "hospital": "安心医疗预约挂号系统",
                    "hospital_level": "三级甲等",
                    "source": "医院官网及公开信息整理",
                    "last_updated": "2025-06-11",
                    "working_hours": {
                        "outpatient_morning": "08:00-12:00",
                        "outpatient_afternoon": "14:00-17:30",
                        "emergency": "24 小时",
                        "special_clinic": {
                            "health_management": "07:30-16:30",
                            "emergency_department": "24/7"
                        }
                    },
                    "registration_info": {
                        "methods": [
                            "微信小程序：安心医疗预约挂号系统",
                            "支付宝小程序：安心医疗预约挂号系统",
                            "电话预约：028-114",
                            "医院官网预约",
                            "现场自助机预约",
                            "门诊窗口预约"
                        ],
                        "requirements": "实名制电子健康卡（需身份证或医保卡办理）",
                        "booking_period": "提前 1-14 天预约",
                        "special_needs_contact": "028-114（轮椅 / 导诊预约）"
                    },
                    "departments": {
                        "emergency": {
                            "name": "急诊急救中心",
                            "level": "国家临床重点专科",
                            "description": "24 小时开放，国家级急诊医学重点专科，配备五大救治中心及国家紧急医学救援队，提供 5G 危重症双向转诊服务",
                            "subsections": [
                                {
                                    "name": "五大救治中心",
                                    "items": [
                                        "胸痛中心（国家级认证）",
                                        "卒中中心（高级卒中中心）",
                                        "创伤中心（区域性创伤救治中心）",
                                        "危重症孕产妇救治中心",
                                        "急性上消化道出血中心"
                                    ]
                                },
                                {
                                    "name": "核心功能区",
                                    "items": [
                                        "急诊抢救室（含 DSA/CT）",
                                        "EICU 急诊重症监护病房（30 张床位）",
                                        "急诊内科病房",
                                        "急诊外科病房"
                                    ]
                                },
                                {
                                    "name": "特色服务",
                                    "items": [
                                        "10 分钟快速检伤分诊",
                                        "危重症患者绿色通道",
                                        "国家紧急医学救援队（可应对突发公共卫生事件）"
                                    ]
                                }
                            ],
                            "doctors": 35,
                            "schedule": "24 小时",
                            "specialties": [
                                "急性胸痛",
                                "脑卒中急救",
                                "严重创伤救治",
                                "危重症妊娠处理",
                                "急性消化道出血"
                            ]
                        },
                        "outpatient": [
                            {
                                "level1": "内科系统",
                                "level2": "呼吸与危重症医学科",
                                "level3": "国家临床重点专科",
                                "description": "擅长哮喘、肺炎、COPD、肺癌早期筛查及呼吸介入治疗，配备肺功能检查室及呼吸重症监护病房",
                                "doctors": 18,
                                "schedule": "周一至周六 08:00-17:30",
                                "availableSlots": {
                                    "2025-06-11": 15,
                                    "2025-06-12": 12,
                                    "2025-06-13": 10
                                },
                                "specialties": [
                                    "哮喘规范化治疗",
                                    "肺癌早期筛查",
                                    "呼吸介入治疗",
                                    "慢性阻塞性肺疾病管理",
                                    "呼吸危重症救治"
                                ]
                            },
                            {
                                "level1": "内科系统",
                                "level2": "心血管内科",
                                "level3": "国家临床重点专科",
                                "description": "专注冠心病、高血压、心力衰竭、心律失常诊疗，拥有心导管室及心电生理实验室，开展冠脉介入及起搏器植入术",
                                "doctors": 22,
                                "schedule": "周一至周五 08:00-17:30，周六 08:00-12:00",
                                "availableSlots": {
                                    "2025-06-11": 18,
                                    "2025-06-12": 15,
                                    "2025-06-13": 12
                                },
                                "specialties": [
                                    "冠心病介入治疗",
                                    "心律失常射频消融",
                                    "心力衰竭规范化管理",
                                    "高血压精准治疗",
                                    "心脏起搏器植入"
                                ]
                            },
                            {
                                "level1": "内科系统",
                                "level2": "神经内科",
                                "level3": "国家临床重点专科",
                                "description": "诊治脑卒中、癫痫、帕金森病等神经系统疾病，拥有卒中单元及神经电生理检查室，提供急性脑卒中 4.5 小时内溶栓治疗",
                                "doctors": 25,
                                "schedule": "周一至周五 08:00-17:30，周六 08:00-12:00",
                                "availableSlots": {
                                    "2025-06-11": 20,
                                    "2025-06-12": 18,
                                    "2025-06-13": 15
                                },
                                "specialties": [
                                    "急性脑卒中溶栓",
                                    "癫痫规范化治疗",
                                    "帕金森病综合管理",
                                    "神经康复治疗",
                                    "周围神经病诊疗"
                                ]
                            },
                            {
                                "level1": "外科系统",
                                "level2": "骨科",
                                "level3": "四川省重点专科",
                                "description": "诊治骨折、关节炎、脊柱疾病、运动损伤，开展关节置换及脊柱微创手术，配备骨科康复一体化团队",
                                "doctors": 28,
                                "schedule": "周一至周六 08:00-17:30",
                                "availableSlots": {
                                    "2025-06-11": 22,
                                    "2025-06-12": 20,
                                    "2025-06-13": 18
                                },
                                "specialties": [
                                    "脊柱微创手术",
                                    "关节置换术",
                                    "复杂骨折内固定",
                                    "运动损伤康复",
                                    "骨肿瘤综合治疗"
                                ]
                            },
                            {
                                "level1": "外科系统",
                                "level2": "神经外科",
                                "level3": "国家临床重点专科",
                                "description": "治疗脑肿瘤、脑出血、脊髓损伤等，配备神经导航及显微手术设备，开展颅内肿瘤切除术及脑血管病介入治疗",
                                "doctors": 20,
                                "schedule": "周一至周五 08:00-17:30",
                                "availableSlots": {
                                    "2025-06-11": 12,
                                    "2025-06-12": 10,
                                    "2025-06-13": 8
                                },
                                "specialties": [
                                    "脑肿瘤显微手术",
                                    "脑出血微创手术",
                                    "脊髓肿瘤切除术",
                                    "神经介入治疗",
                                    "功能神经外科"
                                ]
                            },
                            {
                                "level1": "专科门诊",
                                "level2": "眼科",
                                "level3": "国家临床重点专科",
                                "description": "诊治白内障、青光眼、视网膜疾病、屈光不正，开展飞秒激光近视手术及眼底病微创治疗",
                                "doctors": 18,
                                "schedule": "周一至周六 08:00-17:30",
                                "availableSlots": {
                                    "2025-06-11": 15,
                                    "2025-06-12": 12,
                                    "2025-06-13": 10
                                },
                                "specialties": [
                                    "白内障超声乳化",
                                    "青光眼微创手术",
                                    "飞秒激光近视矫正",
                                    "眼底病玻璃体注药",
                                    "视网膜脱离修复"
                                ]
                            },
                            {
                                "level1": "专科门诊",
                                "level2": "器官移植中心（肝 / 肾）",
                                "level3": "省级重点专科",
                                "description": "开展肝移植、肾移植及移植后管理，拥有移植重症监护病房，累计完成器官移植手术超 2000 例",
                                "doctors": 15,
                                "schedule": "周一至周五 08:00-17:30",
                                "availableSlots": {
                                    "2025-06-11": 8,
                                    "2025-06-12": 6,
                                    "2025-06-13": 5
                                },
                                "specialties": [
                                    "同种异体肝移植",
                                    "肾移植手术",
                                    "移植后抗排斥治疗",
                                    "终末期肝病治疗",
                                    "尿毒症替代治疗"
                                ]
                            },
                            {
                                "level1": "特色诊疗中心",
                                "level2": "健康管理中心",
                                "level3": "全国 TOP2",
                                "description": "提供个性化体检套餐、肿瘤早筛、心脑血管风险评估，配备胶囊胃镜及低剂量螺旋 CT 筛查设备",
                                "doctors": 35,
                                "schedule": "周一至周六 07:30-16:30",
                                "availableSlots": {
                                    "2025-06-11": 60,
                                    "2025-06-12": 55,
                                    "2025-06-13": 50
                                },
                                "specialties": [
                                    "全身肿瘤筛查",
                                    "心脑血管疾病风险评估",
                                    "慢性病健康管理",
                                    "高端体检套餐",
                                    "基因检测服务"
                                ]
                            },
                            {
                                "level1": "特色诊疗中心",
                                "level2": "心身医学中心",
                                "description": "诊治抑郁症、焦虑症、失眠、躯体形式障碍，提供心理咨询、认知行为治疗及药物干预",
                                "doctors": 15,
                                "schedule": "周一至周五 08:00-17:30，周六 08:00-12:00",
                                "availableSlots": {
                                    "2025-06-11": 10,
                                    "2025-06-12": 8,
                                    "2025-06-13": 6
                                },
                                "specialties": [
                                    "抑郁症规范化治疗",
                                    "焦虑障碍认知行为治疗",
                                    "失眠综合干预",
                                    "心身疾病整体调节",
                                    "青少年心理问题干预"
                                ]
                            }
                        ]
                    },
                    "doctors": [
                        {
                            "name": "张明远",
                            "department": "心血管内科",
                            "title": "主任医师",
                            "specialty": "冠心病介入治疗、心力衰竭",
                            "experience": "28 年",
                            "schedule": {
                                "Monday": [
                                    "08:30-11:30（专家门诊）",
                                    "14:00-16:30（普通门诊）"
                                ],
                                "Wednesday": [
                                    "09:00-11:00（介入手术）",
                                    "14:30-16:30（专家门诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（大查房）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 8,
                                "2025-06-13": 6
                            },
                            "consultation_fee": 120,
                            "awards": [
                                "国家临床重点专科带头人",
                                "国务院特殊津贴专家"
                            ],
                            "special_tags": [
                                "学科带头人",
                                "冠心病首席专家"
                            ]
                        },
                        {
                            "name": "李慧",
                            "department": "心血管内科",
                            "title": "副主任医师",
                            "specialty": "心律失常、高血压",
                            "experience": "16 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（普通门诊）",
                                    "14:00-16:00（专科门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（心电生理检查）",
                                    "15:00-17:00（专家门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（高血压专病门诊）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 10,
                                "2025-06-14": 8
                            },
                            "consultation_fee": 80,
                            "awards": [
                                "省级青年医学人才",
                                "心律失常诊疗标兵"
                            ],
                            "special_tags": [
                                "高血压专科医生",
                                "心律失常专家"
                            ]
                        },
                        {
                            "name": "王劲松",
                            "department": "神经内科",
                            "title": "主任医师",
                            "specialty": "脑卒中、癫痫",
                            "experience": "25 年",
                            "schedule": {
                                "Monday": [
                                    "09:00-11:00（卒中门诊）",
                                    "14:30-16:30（专家门诊）"
                                ],
                                "Wednesday": [
                                    "08:30-10:30（溶栓治疗指导）",
                                    "14:00-16:00（癫痫专病门诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（神经电生理会诊）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 6,
                                "2025-06-13": 4
                            },
                            "consultation_fee": 120,
                            "awards": [
                                "国家临床重点专科骨干",
                                "脑卒中防治专家"
                            ],
                            "special_tags": [
                                "卒中中心负责人",
                                "癫痫诊疗权威"
                            ]
                        },
                        {
                            "name": "赵文慧",
                            "department": "神经内科",
                            "title": "副主任医师",
                            "specialty": "帕金森病、头痛",
                            "experience": "18 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（帕金森专病门诊）",
                                    "14:00-16:00（普通门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（神经康复指导）",
                                    "15:00-17:00（头痛专科门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（记忆门诊）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 8,
                                "2025-06-14": 6
                            },
                            "consultation_fee": 80,
                            "awards": [
                                "省级神经内科青年专家",
                                "帕金森病诊疗先进个人"
                            ],
                            "special_tags": [
                                "帕金森专科医生",
                                "头痛诊疗专家"
                            ]
                        },
                        {
                            "name": "陈建国",
                            "department": "骨科",
                            "title": "主任医师",
                            "specialty": "脊柱外科、关节置换",
                            "experience": "30 年",
                            "schedule": {
                                "Monday": [
                                    "08:30-11:30（脊柱专家门诊）",
                                    "14:00-16:30（大查房）"
                                ],
                                "Wednesday": [
                                    "09:00-11:00（手术）",
                                    "14:30-16:30（关节专科门诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（疑难病例会诊）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 5,
                                "2025-06-13": 3
                            },
                            "consultation_fee": 150,
                            "awards": [
                                "四川省重点专科带头人",
                                "脊柱外科终身成就奖"
                            ],
                            "special_tags": [
                                "脊柱外科首席专家",
                                "关节置换权威"
                            ]
                        },
                        {
                            "name": "林晓",
                            "department": "骨科",
                            "title": "副主任医师",
                            "specialty": "运动损伤、骨折微创治疗",
                            "experience": "15 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（运动损伤门诊）",
                                    "14:00-16:00（普通门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（骨折复位）",
                                    "15:00-17:00（微创治疗门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（骨科康复指导）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 10,
                                "2025-06-14": 8
                            },
                            "consultation_fee": 70,
                            "awards": [
                                "省级骨科青年专家",
                                "运动医学先进工作者"
                            ],
                            "special_tags": [
                                "运动损伤专科医生",
                                "骨折微创专家"
                            ]
                        },
                        {
                            "name": "郑立伟",
                            "department": "神经外科",
                            "title": "主任医师",
                            "specialty": "脑肿瘤、脑出血",
                            "experience": "25 年",
                            "schedule": {
                                "Monday": [
                                    "09:30-11:30（脑肿瘤门诊）",
                                    "14:30-16:30（专家门诊）"
                                ],
                                "Wednesday": [
                                    "08:30-10:30（手术）",
                                    "14:00-16:00（脑出血专病门诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（神经影像会诊）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 4,
                                "2025-06-13": 2
                            },
                            "consultation_fee": 120,
                            "awards": [
                                "国家临床重点专科核心成员",
                                "脑肿瘤诊疗专家"
                            ],
                            "special_tags": [
                                "脑肿瘤首席专家",
                                "神经外科带头人"
                            ]
                        },
                        {
                            "name": "吴敏",
                            "department": "神经外科",
                            "title": "副主任医师",
                            "specialty": "脊髓损伤、功能神经外科",
                            "experience": "17 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（脊髓损伤门诊）",
                                    "14:00-16:00（普通门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（神经电生理检查）",
                                    "15:00-17:00（功能神经外科门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（术后康复指导）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 6,
                                "2025-06-14": 4
                            },
                            "consultation_fee": 80,
                            "awards": [
                                "神经外科青年技术标兵",
                                "脊髓损伤诊疗先进个人"
                            ],
                            "special_tags": [
                                "脊髓损伤专科医生",
                                "功能神经外科专家"
                            ]
                        },
                        {
                            "name": "刘光明",
                            "department": "眼科",
                            "title": "主任医师",
                            "specialty": "白内障、眼底病",
                            "experience": "22 年",
                            "schedule": {
                                "Monday": [
                                    "08:30-11:30（白内障专家门诊）",
                                    "14:00-16:30（手术）"
                                ],
                                "Wednesday": [
                                    "09:00-11:00（眼底病门诊）",
                                    "14:30-16:30（专家门诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（眼底激光治疗）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 8,
                                "2025-06-13": 6
                            },
                            "consultation_fee": 100,
                            "awards": [
                                "国家临床重点专科骨干",
                                "白内障超声乳化专家"
                            ],
                            "special_tags": [
                                "白内障首席专家",
                                "眼底病诊疗权威"
                            ]
                        },
                        {
                            "name": "孙莉",
                            "department": "眼科",
                            "title": "副主任医师",
                            "specialty": "近视矫正、青光眼",
                            "experience": "15 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（近视矫正门诊）",
                                    "14:00-16:00（普通门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（角膜检查）",
                                    "15:00-17:00（青光眼专科门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（飞秒激光手术）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 10,
                                "2025-06-14": 8
                            },
                            "consultation_fee": 70,
                            "awards": [
                                "眼科青年技术能手",
                                "近视矫正先进个人"
                            ],
                            "special_tags": [
                                "近视矫正专家",
                                "青光眼专科医生"
                            ]
                        },
                        {
                            "name": "黄志强",
                            "department": "器官移植中心",
                            "title": "主任医师",
                            "specialty": "肝移植、终末期肝病",
                            "experience": "20 年",
                            "schedule": {
                                "Monday": [
                                    "08:30-11:30（肝移植门诊）",
                                    "14:00-16:30（大查房）"
                                ],
                                "Wednesday": [
                                    "09:00-11:00（手术）",
                                    "14:30-16:30（肝病专家门诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（移植后随访）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 3,
                                "2025-06-13": 2
                            },
                            "consultation_fee": 150,
                            "awards": [
                                "省级器官移植学科带头人",
                                "肝移植手术标兵"
                            ],
                            "special_tags": [
                                "肝移植首席专家",
                                "器官移植权威"
                            ]
                        },
                        {
                            "name": "周琳",
                            "department": "器官移植中心",
                            "title": "副主任医师",
                            "specialty": "肾移植、尿毒症",
                            "experience": "13 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（肾移植门诊）",
                                    "14:00-16:00（普通门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（透析指导）",
                                    "15:00-17:00（尿毒症专科门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（移植后抗排斥指导）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 5,
                                "2025-06-14": 4
                            },
                            "consultation_fee": 80,
                            "awards": [
                                "器官移植青年专家",
                                "肾移植诊疗先进个人"
                            ],
                            "special_tags": [
                                "肾移植专科医生",
                                "尿毒症治疗专家"
                            ]
                        },
                        {
                            "name": "钱卫",
                            "department": "健康管理中心",
                            "title": "主任医师",
                            "specialty": "肿瘤早筛、慢性病管理",
                            "experience": "25 年",
                            "schedule": {
                                "Monday": [
                                    "08:00-11:30（肿瘤筛查门诊）",
                                    "14:00-16:30（健康评估）"
                                ],
                                "Wednesday": [
                                    "08:30-11:30（慢性病管理门诊）",
                                    "14:30-16:30（专家会诊）"
                                ],
                                "Friday": [
                                    "08:00-10:00（体检报告解读）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-11": 15,
                                "2025-06-13": 12
                            },
                            "consultation_fee": 100,
                            "awards": [
                                "全国健康管理先进个人",
                                "肿瘤早筛专家"
                            ],
                            "special_tags": [
                                "健康管理学科带头人",
                                "肿瘤早筛权威"
                            ]
                        },
                        {
                            "name": "冯婷",
                            "department": "心身医学中心",
                            "title": "副主任医师",
                            "specialty": "抑郁症、焦虑症",
                            "experience": "12 年",
                            "schedule": {
                                "Tuesday": [
                                    "09:30-11:30（抑郁症门诊）",
                                    "14:00-16:00（普通门诊）"
                                ],
                                "Thursday": [
                                    "08:00-10:00（心理测评）",
                                    "15:00-17:00（焦虑障碍专科门诊）"
                                ],
                                "Saturday": [
                                    "08:30-10:30（心理咨询）"
                                ]
                            },
                            "availableSlots": {
                                "2025-06-12": 8,
                                "2025-06-14": 6
                            },
                            "consultation_fee": 70,
                            "awards": [
                                "心身医学青年专家",
                                "心理咨询先进个人"
                            ],
                            "special_tags": [
                                "抑郁症专科医生",
                                "焦虑障碍治疗专家"
                            ]
                        }
                    ],
                    "appointment_rules": {
                        "cancellation_policy": {
                            "more_than_24h": "免费取消",
                            "within_24h": "收取 50% 挂号费",
                            "no_show": "视为爽约，累计 3 次限制预约 30 天"
                        },
                        "late_arrival": "迟到超 30 分钟预约失效，需重新挂号",
                        "required_documents": [
                            "身份证原件",
                            "电子健康卡",
                            "医保卡（如需医保报销）"
                        ],
                        "payment_methods": [
                            "微信支付",
                            "支付宝",
                            "银行卡",
                            "现金",
                            "医保统筹支付"
                        ],
                        "special_services": {
                            "wheelchair": "门诊一楼导诊台免费借用，需身份证抵押",
                            "language_services": "提供英语 / 手语翻译，需提前 48 小时致电 028-114 预约",
                            "home_visit": "针对术后康复患者提供上门评估（仅限距医院 10 公里内）"
                        }
                    },
                    "special_notes": [
                        "急诊患者无需预约，直接到急诊楼分诊，危急重症优先救治",
                        "国家临床重点专科（心血管内科、神经内科、神经外科、眼科）开设绿色通道",
                        "智能导诊机器人可通过微信小程序呼叫，提供科室导航服务",
                        "健康管理中心体检需空腹，建议早晨 7:30-10:00 到达"
                    ],
                    "faq": [
                        {
                            "question": "如何确认我的预约是否成功？",
                            "answer": "预约成功后，您将收到短信通知，同时可在微信 / 支付宝小程序 ' 我的预约 ' 中查看。预约号源以系统实时显示为准，若未收到短信可致电 028-114 查询。"
                        },
                        {
                            "question": "国家临床重点专科如何挂号？",
                            "answer": "可通过各预约渠道直接选择标注 ' 国家临床重点专科 ' 的科室，部分专家号需提前 7 天放号。危急重症患者可通过急诊绿色通道优先就诊。"
                        },
                        {
                            "question": "器官移植中心如何申请预约？",
                            "answer": "器官移植评估需先到门诊三楼器官移植中心初诊，由接诊医生判断是否符合移植条件，再安排后续预约。急诊移植可直接联系科室值班电话 028-12345678。"
                        },
                        {
                            "question": "健康管理中心体检需要注意什么？",
                            "answer": "体检前需禁食 8-12 小时，避免剧烈运动；穿着宽松衣物，勿穿带有金属饰品的衣服；女性用户需避开月经期。体检报告可通过微信小程序查询，或在体检后 3-5 个工作日到中心领取。"
                        }
                    ],
                    "locations": {
                        "address": "四川省成都市青羊区一环路西二段 32 号",
                        "building_layout": {
                            "emergency_building": "独立建筑，医院大门右侧（含急诊抢救室、EICU）",
                            "outpatient_building": "1-5 层，正门入口（1 楼挂号收费，2-4 楼专科门诊，5 楼特色诊疗中心）",
                            "medical_technology_building": "门诊楼后方（1 楼检验科，2-3 楼影像科，4 楼病理科）",
                            "surgery_building": "门诊楼东侧（3 楼普通外科，4 楼骨科，5 楼神经外科，6 楼心脏大血管外科）",
                            "organ_transplant_center": "外科楼 7 楼（独立病区）"
                        },
                        "department_locations": {
                            "心血管内科": "内科楼 3 楼东区",
                            "神经内科": "内科楼 4 楼东区",
                            "骨科": "外科楼 4 楼",
                            "神经外科": "外科楼 5 楼",
                            "眼科": "门诊楼 4 楼西区",
                            "器官移植中心": "外科楼 7 楼",
                            "健康管理中心": "门诊楼 5 楼整层",
                            "心身医学中心": "门诊楼 4 楼东区"
                        },
                        "transportation": {
                            "subway": "地铁 2 号线通惠门站 A 出口，步行 500 米（约 8 分钟）",
                            "bus_stops": [
                                "一环路西二段站（27 路、34 路、82 路）",
                                "中医附院站（11 路、42 路、58 路、64 路）"
                            ],
                            "parking": {
                                "underground": "医院地下停车场（5 元 / 小时，30 元 / 天封顶）",
                                "nearby": [
                                    "宽窄巷子停车场",
                                    "四川国际大厦停车场"
                                ]
                            }
                        }
                    }
                }
        
        如果用户询问非预约挂号相关问题（如医疗诊断、药品咨询等），
        请礼貌拒绝并说明："抱歉，我仅处理预约挂号相关事宜"。
        请用中文简洁清晰地回复问题。
        """;


    /**
     * 聊天接口
     *
     * @param message
     * @return
     */
    @Operation(summary = "AI对话")
    @GetMapping("/ds")
    public String generate(@RequestParam(value = "message", defaultValue = "hello") String message) {
        Prompt prompt = createPrompt(message);
        ChatResponse response = chatModel.call(prompt);
        return extractText(response);
    }

    /**
     * 流式聊天接口
     * @param message
     * @return
     */
    @GetMapping("/ds/chatStream")
    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "hello") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatModel.stream(prompt).map(this::extractText);
    }


    /**
     * 从ChatResponse提取纯文本
     */
    private String extractText(ChatResponse response) {
        return response.getResults().stream()
                .findFirst()
                .map(result -> result.getOutput().getText())
                .orElse("无法生成回复");
    }


    private Prompt createPrompt(String userQuestion) {
        return new Prompt(List.of(
                new SystemMessage(SYSTEM_PROMPT),
                new UserMessage(userQuestion)
        ));
    }

}
